/**
 * 
 */
package edu.gtcfla.km.appmodules.vo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.vsg.common.page.AbstractPage;
import org.vsg.search.api.page.PaginationException;

/**
 * @author vison
 *
 */
public class DefaultPage<T extends java.io.Serializable> extends AbstractPage<T> {

	private final static Logger logger = LoggerFactory.getLogger(DefaultPage.class);	
	
	@Override
	public void initAfterPropSet() throws PaginationException {
		// TODO Auto-generated method stub
		int totalRecord = this.totalRecord;
		int totalPages = (int)Math.ceil( ((double)totalRecord )/this.pageSize);
		setTotalPages( totalPages );
		
		
		if (this.totalPages > 0) {
			this.setLastPageIndex( this.totalPages );
		}
		
		int pageNumber = getPageIndex();

		if (pageNumber > this.getLastPageIndex() ) {
			if (totalRecord != 0) {
				StringBuilder warning = new StringBuilder("Page inputed is over the max page number!");
				logger.warn(warning.toString());
			}
			// fix the pageIndex number
			this.setPageIndex( this.getLastPageIndex() );
		} else if (pageNumber < this.getFirstPageIndex()) {
			logger.warn(expMessage_gotoPageUnderTotalPages);
			// fix the pageIndex number
			this.setPageIndex( this.getFirstPageIndex() );
			
		}				
	}

	
	
}
